package com.gis.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gis.entity.OrganismData;
import com.gis.entity.Publish;
import com.gis.mapper.OrganismDataMapper;
import com.gis.mapper.PublishMapper;
import com.gis.properties.GeoserverProperties;
import com.gis.service.IOrganismDataService;
import com.gis.service.IPublishService;
import com.gis.utils.GeoserverUtil;
import com.gis.vo.PublishVO;
import it.geosolutions.geoserver.rest.GeoServerRESTManager;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.net.MalformedURLException;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author bobo
 * @since 2024-11-02
 */
@Service
@RequiredArgsConstructor
public class OrganismDataServiceImpl extends ServiceImpl<OrganismDataMapper, OrganismData> implements IOrganismDataService {
    private final IPublishService publishService;
    private final PublishMapper publishMapper;
    private final GeoserverUtil geoserverUtil;
    private final GeoserverProperties geoserverProperties;

    @Override
    public List<PublishVO> getOrgPublishUrl() {
        List<Publish> publishByType = publishService.getPublishByType(Publish.Organism);
        return publishByType.stream()
                .map(publish -> {
                    PublishVO publishVO = new PublishVO();
                    BeanUtils.copyProperties(publish, publishVO);
                    return publishVO;
                })
                .collect(Collectors.toList());
    }

    @Override
    public int publishLayer(String name) {
        int success = 0;
        try {
            // 1. 发布生物数据表图层
            GeoServerRESTManager geoServerRESTManager  = geoserverUtil.getManager();
            String workspace = "webgis";
            String storeName = "org";
            String styleName = "public";
            String nativeName = "organism_data";
            String srs = "EPSG:4326";
            if(geoserverUtil.createWorkArea(geoServerRESTManager,workspace))
                System.out.println("工作空间创建成功");
            if(geoserverUtil.createDBDataPool(geoServerRESTManager,workspace,storeName))
                System.out.println("存储仓库创建成功");
            boolean publishResult = geoserverUtil.publishDBLayer(geoServerRESTManager, workspace, storeName, styleName, name, nativeName, srs);
            if(publishResult){
                // 2. 保存到图层表中
                String publishType = "生物";
                // 2. 构造发布的 URL
                String publishUrl = String.format(
                    "%s%s/ows?service=WFS&version=1.0.0&request=GetFeature&typeName=%s:%s&maxFeatures=%d&outputFormat=application/json",
                    geoserverProperties.getGeoserver_url(), workspace, workspace, name, 2000
                );
                Publish publish = new Publish(null,name,publishType,publishUrl);
                success = publishMapper.insert(publish);
            }
        } catch (MalformedURLException e) {
            throw new RuntimeException(e);
        }
        return success;
    }
}
